package keeto.com.vn.classified.adapter;


import java.util.Vector;

import keeto.com.vn.classfied.model.RatingProduct;
import keeto.com.vn.classified.ui.R;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ProgressBar;
import android.widget.TextView;


/**
 * @category (一级)分类信息适配器CategoriesAdapterManager中用到
 * */
public class RatingProductAdapter extends BaseAdapter implements IRatingProductsAdapter{
	
	class ViewHolder {
		private TextView tvRatingName;
		private ProgressBar mBarRating;
		private TextView tvRatingNumber;
	}
	/**
	 * @category 数据源
	 * */
	private Vector<RatingProduct> dataSource;
	private Context context;



	public RatingProductAdapter(Vector<RatingProduct> dataSource, Context context) {
		this.dataSource = dataSource;
		this.context = context;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public void setDataSource(Vector<?> dataSource) {
		this.dataSource = (Vector<RatingProduct>) dataSource;
	}

	@Override
	public int getCount() {
		if (dataSource == null || dataSource.size() == 0) {
			return 0;
		} else {
			return dataSource.size();
		}
	}

	@Override
	public Object getItem(int position) {
		return dataSource.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (dataSource == null || dataSource.size() == 0) {

			return null;

		} else {

			RatingProduct rating = dataSource.get(position);

			ViewHolder holder = null;
			View view = convertView;

			if (view != null && view.getTag() != null) {
				holder = (ViewHolder) view.getTag();

			} else {

				view = LayoutInflater.from(context).inflate(R.layout.rating_view, parent, false);
				holder = new ViewHolder();
				holder.tvRatingName = (TextView) view.findViewById(R.id.rating_name);
				holder.mBarRating = (ProgressBar)view.findViewById(R.id.pBar_rating);
				holder.tvRatingNumber = (TextView) view.findViewById(R.id.rating_number);
				view.setTag(holder);
				holder.tvRatingName.setText(rating.getRatingName());
				holder.mBarRating.setProgress((int) rating.getRatingPercent());     
				holder.tvRatingNumber.setText(rating.getRatingNumber());
			}
			
			return view;
		}
	}
	
}
